

package com.springdemo.tools;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 数据工具类
 * @author ss
 * @date   2013-10-10
 */
public class NumberHelper {
	
	static String regEx = "[\u4e00-\u9fa5]";
	
	static String rex =  "^\\d*[0-9]\\d*$";
	static Pattern p = Pattern.compile(rex);
	/**
	 * 传过来的不得为null
	 * @Title: getNull
	 * @data:2014-4-15上午11:16:42
	 * @author:san
	 *
	 * @return
	 */
	public static int getNull(Integer num){
		int result = 0;
		if(num==null){
			return result;
		}else{
			return num;
		}
	}

    public static float getFloatNull(Float num){
        float result = 0;
        if(num==null){
            return result;
        }else{
            return num;
        }
    }
	public static boolean isNum(String str) {
		Matcher matcher = p.matcher(str);
		boolean flg = false;
		if (matcher.find())    {
			flg = true;
		}
		return flg;
	}
	
	
	/**
	 * 字符串转整形
	 * @param num
	 * @return
	 */
	public static int stringToInt(String num){
		int result = 0;
		try{
			result = Integer.parseInt(num);
		}catch(Exception ex){
			result = 0;
		}
		return result;
	}
	/**
	 * 字符串转整形(出错时设置默认值)
	 * @param num
	 * @return
	 */
	public static int stringToInt(String num,int defult){
		int result = 0;
		try{
			result = Integer.parseInt(num);
		}catch(Exception ex){
			result = defult;
		}
		return result;
	}

    /**
     * 字符串转成long
     * @param num
     * @return
     */
    public static long stringToLong(String num){
        long result = 0;
        try{
            result = Long.parseLong(num);
        }catch(Exception ex){
            result = 0;
        }
        return result;
    }




	
	/**
	 * 将字符串转换成浮点型，转换失败时返回0
	 * @author xieyan
	 * @date   2012-6-3下午06:06:33
	 * @param src
	 * @return 
	 */
	public static float stringToFloat(String src){
		float result = 0;
		try{
			result = Float.parseFloat(src);			
		}catch(Exception ex){
			
		}
		return result;
	}
	/**
	 * 将float后面两个保留两位
	 * @Title: floadToString
	 * @data:2012-7-18下午05:44:57
	 * @author:zxd
	 *
	 * @param fc
	 * @return
	 */
	public static String floadToString(float fc){
		DecimalFormat   fnum   =   new   DecimalFormat("##0.00");
		String   dd=fnum.format(fc);      
		return dd;
	}
	/**
	 * 将字符转换成浮点型，如果失败返回传入的参数
	 * @author xieyan
	 * @date   2012-6-3下午06:09:08
	 * @param src
	 * @param defult
	 * @return
	 */
	public static float stringToFloat(String src,float defult){
		float result = 0;
		try{
			result = Float.parseFloat(src);	
			
		}catch(Exception ex){
			result = defult;
		}
		return result;
	}
	/**
	 * 字符转成double
	 * @Title: stringToDouble
	 * @data:2012-7-20上午10:30:15
	 * @author:zxd
	 *
	 * @param src
	 * @return
	 */
	public static double stringToDouble(String src){
		double result = 0;
		try{
			result = Double.parseDouble(src);	
			
		}catch(Exception ex){
		}
		return result;
	}

	/**
	 * 精确四舍五入
	 * @Title: round
	 * @data:2012-7-18下午06:36:18
	 * @author:zxd
	 *
	 * @param v
	 * @param scale 小数点后溜几位
	 * @return
	 */
	public static float round(double v,int scale){
        if(scale<0){
            throw new IllegalArgumentException(
                "The scale must be a positive integer or zero");
        }
        BigDecimal b = new BigDecimal(Double.toString(v));
        BigDecimal one = new BigDecimal("1");
        return b.divide(one,scale,BigDecimal.ROUND_HALF_UP).floatValue();
    }

    /**
     * 返回共有几位小数
     * @param nums
     * @return
     */
    public static int floatNums(String nums){
        int result = 0;
          if(nums!=null && !nums.equals("")){
              int pointN = nums.indexOf(".");
              int position = nums.length() - (pointN+ 1);
              result =position;
          }
        return result;
    }


    /**
     * 前面启动补0
     * @return
     */
    public static String formatNumber(int youNumber){
        String str = "000";
        if(youNumber>0){
            str = String.format("%03d", youNumber);
        }
        return str;
    }

    public static double rounds(double v,int scale){
        if(scale<0){
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b = new BigDecimal(v);
        BigDecimal one = new BigDecimal("1");
        return b.divide(one,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
    }
	
    /**
     * 字节转换
     */
	public static String getSizeDesc(long size) {
		String suffix = "B";
		if (size >= 1024) {
			suffix = "K";
			size = size >> 10;
			if (size >= 1024) {
				suffix = "M";
				size = size >> 10;
				if (size >= 1024) {
					suffix = "G";
					size = size >> 10;
				}
			}
		}
		return size + suffix;
	}
	
	/**
	 * 判断是否为整数
	 * @Title: isInteger
	 * @data:2015-10-22下午2:42:53
	 * @author:charlin_yin
	 *
	 * @param value
	 * @return
	 */
	public static boolean isInteger(String value){
		String regEx = "^[0-9]*[1-9][0-9]*$";
		Matcher matcher = Pattern.compile(regEx).matcher(value);
		boolean flg = false;
		if (matcher.find()){
		flg = true;
		}
		return flg;
	}

    /**
     * 两数相加
     * @param arg1
     * @param arg2
     * @return
     */
    public static float twoAdd(float arg1,float arg2){
        BigDecimal b1 = new BigDecimal(Float.toString(arg1));
        BigDecimal b2 = new BigDecimal(Float.toString(arg2));
        return b1.add(b2).floatValue();
    } /**
     * 两数相加
     * @param arg1
     * @param arg2
     * @return
     */
    public static float twoFloatAdd(float arg1,float arg2){
        BigDecimal b1 = new BigDecimal(Float.toString(arg1));
        BigDecimal b2 = new BigDecimal(Float.toString(arg2));
        return b1.add(b2).floatValue();
    }

    /**
     * 三个数相加
     * @param arg1
     * @param arg2
     * @param arg3
     * @return
     */
    public static float threeAdd(float arg1,float arg2,float arg3){
        BigDecimal b1 = new BigDecimal(Float.toString(arg1));
        BigDecimal b2 = new BigDecimal(Float.toString(arg2));
        BigDecimal b3 = new BigDecimal(Float.toString(arg3));
        return b1.add(b2).add(b3).floatValue();
    }



    public static float fourAdd(float arg1,float arg2,float arg3,float arg4){
        BigDecimal b1 = new BigDecimal(Float.toString(arg1));
        BigDecimal b2 = new BigDecimal(Float.toString(arg2));
        BigDecimal b3 = new BigDecimal(Float.toString(arg3));
        BigDecimal b4 = new BigDecimal(Float.toString(arg4));
        return b1.add(b2).add(b3).add(b4).floatValue();
    }


    public static float fourSix(float arg1,float arg2,float arg3,float arg4,float arg5,float arg6){
        BigDecimal b1 = new BigDecimal(Float.toString(arg1));
        BigDecimal b2 = new BigDecimal(Float.toString(arg2));
        BigDecimal b3 = new BigDecimal(Float.toString(arg3));
        BigDecimal b4 = new BigDecimal(Float.toString(arg4));
        BigDecimal b5 = new BigDecimal(Float.toString(arg5));
        BigDecimal b6 = new BigDecimal(Float.toString(arg6));
        return b1.add(b2).add(b3).add(b4).add(b5).add(b6).floatValue();
    }

    public static float fourFive(float arg1,float arg2,float arg3,float arg4,float arg5){
        BigDecimal b1 = new BigDecimal(Float.toString(arg1));
        BigDecimal b2 = new BigDecimal(Float.toString(arg2));
        BigDecimal b3 = new BigDecimal(Float.toString(arg3));
        BigDecimal b4 = new BigDecimal(Float.toString(arg4));
        BigDecimal b5 = new BigDecimal(Float.toString(arg5));
        return b1.add(b2).add(b3).add(b4).add(b5).floatValue();
    }

	/**
	 * 提供精确的乘法运算。
	 * @param v1 被乘数
	 * @param v2 乘数
	 * @return 两个参数的积
	 */
	public static float mul(float v1,int v2){
		BigDecimal b1 = new BigDecimal(Double.toString(v1));
		BigDecimal b2 = new BigDecimal(Integer.toString(v2));
		return b1.multiply(b2).floatValue();
	}
	/**
	 * 提供精确的乘法运算。
	 * @param v1 被乘数
	 * @param v2 乘数
	 * @return 两个参数的积
	 */
	public static double mul(double v1,int v2){
		BigDecimal b1 = new BigDecimal(Double.toString(v1));
		BigDecimal b2 = new BigDecimal(Integer.toString(v2));
		return b1.multiply(b2).doubleValue();
	}

	public static void main(String[] args) {
		System.out.println(NumberHelper.formatNumber(85));
	}
}
